package pearson.automation.assets;

import org.openqa.selenium.By;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.RemoteWebElement;

import framework.BaseEntity;
import framework.elements.Button;
import framework.elements.Label;
import framework.elements.Link;

public abstract class BaseAsset extends BaseEntity {
    protected String assetXPath = "//li[contains(@class,'srl-item')][contains(.,'%1$s')]";
    protected String assetXPathWithIndex = "(//li[contains(@class,'srl-item')][contains(.,'%1$s')])[%2$d]";
    protected Button image;
    protected Label smallTitle;
    protected Label bigTitle;
    protected Link url;
    protected Button libraryAction;
    protected Button newCourse;
    protected Button edit;
    protected Button preview;
    protected Button addTo;
    protected Button addToLibrary;
    protected Button remove;
    protected Label element;
    protected Label dragHandler;
    protected Label thumbnailImg;
    
    /**
     * Базовый конструктор для элементов. 
     * Вынес локаторы в конструктор, чтобы не инициализировались раньше времени.
     */
    private void createElements() {
        element = new Label(By.xpath(assetXPath), "Asset");
        image = new Button(By.xpath(String.format("%1$s//%2$s",
                assetXPath, "div[contains(@class,'item-image')]")), "Image");
        smallTitle = new Label(By.xpath(String.format("%1$s//%2$s",
                assetXPath, "h1[@class='gv-title']/a")), "Small title");
        bigTitle = new Label(By.xpath(String.format("%1$s//%2$s",
                assetXPath, "hgroup/h1/a")), "Big title");
        url = new Link(By.xpath(String.format("%1$s//%2$s",
                assetXPath, "a[@title='View details']")), "URL");
        libraryAction = new Button(By.xpath(String.format("%1$s//%2$s",
                assetXPath,
                "li[@class='last cta-library']//span[contains(.,'library')]")),
                "Library actions");
        newCourse = new Button(By.xpath(String.format("%1$s//%2$s",
                assetXPath,
                "li[@class='cta-new']//a[contains(.,'New')]")),
                "Add as new course");
        edit = new Button(By.xpath(String.format("%1$s//%2$s",
                assetXPath,
                "li[@class='cta-edit']//a[contains(.,'Edit')]")), "Edit");
        preview = new Button(By.xpath(String.format("%1$s//%2$s",
                assetXPath, "li//a[contains(.,'Preview')]")), "Preview");
        addTo = new Button(By.xpath(String.format("%1$s//%2$s",
                assetXPath,
                "li[@class='cta-add']//a[contains(.,'Add to')]")),
                "Add to course");
        remove = new Button(By.xpath(String.format("%1$s//%2$s",
                assetXPath, "li[@class='last cta-library']"
              + "//a[@class='library-remove-icon-only']")),
                "Remove");
        addToLibrary = new Button(By.xpath(String.format("%1$s//%2$s",
                assetXPath, "li[@class='last cta-library']"
              + "//a[@class='library-icon-only']")),
                "Add to Library");
        dragHandler = new Label(By.xpath(String.format("%1$s//%2$s",
                assetXPath,
                "div[contains(@class,'drag-handle-box')]")), "Drag handler");
        thumbnailImg = new Label(By.xpath(String.format("%1$s//%2$s",
                assetXPath, "img")), "Asset title");
    }

    protected BaseAsset(String xpath, String assetName) {
        assetXPath = String.format(xpath, assetName);
        createElements();
    }
    
    public void scrollTo() {
        element.getElement().getLocationOnScreenOnceScrolledIntoView();
    }

    protected BaseAsset(String assetName) {
        assetXPath = String.format(assetXPath, assetName);
        createElements();
    }

    protected BaseAsset(String assetName, int index) {
        assetXPath = String.format(assetXPathWithIndex, assetName, index);
        createElements();
    }

    public BaseAsset(String xpath, boolean byXPath) {
        assetXPath = xpath;
        createElements();
    }

    public void dragToElement(RemoteWebElement destinationElement) {
        Actions builder = new Actions(browser.getDriver());
        builder
            .moveToElement(element.getElement())
            .moveToElement(element.getElement())
            .moveToElement(browser.getDriver().findElement(
                    dragHandler.getLocator()))
            .moveToElement(browser.getDriver().findElement(
                    dragHandler.getLocator()))
            .dragAndDrop(browser.getDriver().findElement(
                    dragHandler.getLocator()), destinationElement)
            .build()
            .perform();
    }

    public String getAssetTitle() {
        return thumbnailImg.getElement().getAttribute("title");
    }

    public String getThumbnailURL() {
        return thumbnailImg.getElement().getAttribute("src");
    }

    public boolean hasAddToButton() {
        return addTo.isPresent();
    }

    public boolean hasAddToLibraryButton() {
        return addToLibrary.isPresent();
    }

    public boolean isPresent() {
        return element.isPresent();
    }

    public boolean isPresent(long timeout) {
        return element.isPresent(timeout);
    }

    public void assertIsPresent() {
        element.assertIsPresent();
    }

    public void click(boolean fictive) {
        element.clickAndWait();
    }

    public void clickOnThumbnail() {
        //Avoid roll over here
        Actions builder = new Actions(browser.getDriver());
        builder.moveToElement(image.getElement(), 10, 10)
               .click();
        builder.build().perform();
    }

    public int getThumbnailWidth() {
        String width = thumbnailImg.getElement().getCssValue("width");
        return Integer.valueOf(width.replaceAll("px", ""));
    }

    public int getThumbnailHeigth() {
        String width = thumbnailImg.getElement().getCssValue("height");
        return Integer.valueOf(width.replaceAll("px", ""));
    }

    public void clickOnSmallTitle() {
        smallTitle.clickAndWait();
    }

    public void clickOnBigTitle() {
        bigTitle.clickAndWait();
    }

    public String getAssetURL() {
        return url.getHref();
    }
}
